package mycompany.model.message;

import java.util.Date;
import mycompany.model.BaseModel;
import mycompany.model.UserBaseModel;

/**
 * Represents message base model
 * @author Volodymyr Lavrynovych <volodya.lavrynovych@gmail.com>
 */
public class MessageBaseModel <T extends UserBaseModel> extends BaseModel implements Comparable{
    
    protected T user;
    protected String text;
    private Date date;
    
    public MessageBaseModel(long id, String text, T user){
        super(id);
        if(text==null || text.isEmpty()) throw new NullPointerException("Text is empty");
        this.text = text;
        if(user==null) throw new NullPointerException("User is undefined");
        this.user = user;
        date = new Date();
    }
    
    public Date getDate() {
        return date;
    }
    
    public String getUserName() {
        return user.getName();
    }
    
    public String getUserId() {
        return user.getId();
    }

    public String getText() {
        return text;
    }
    
    public int compareTo(Object o) {
        if(o instanceof MessageBaseModel) {
            return this.date.compareTo(((MessageBaseModel)o).getDate());
        } else {
            return 1;
        }
    }
}
